<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sortNo" placeholder="排序" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="签名标题" prop="title">
				<el-input v-model="form.title" placeholder="签名标题" clearable></el-input>
			</el-form-item>
			<el-form-item label="使用人" prop="accountId">
				<select-user-dialog v-model:accountId="form.accountId" :mode="false"/>
			</el-form-item>
			<el-form-item label="签名文件">
				<sc-upload :apiObj="apiObj" v-model="signPath" title="开启剪裁" :cropper="true" :compress="0.5" :aspectRatio="0" @success="getFileName" style="height: 60px;"></sc-upload>
			</el-form-item>
			<el-form-item label="授权密码" prop="password">
				<el-input type="password" v-model="form.password" placeholder="授权密码" clearable></el-input>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" type="textarea" maxlength="150" clearable ></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import config from "@/config";
	export default {
		emits: ['success', 'closed'],
		components: {
			selectUserDialog,
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				apiObj:this.$API.file.uploadBpmSign,
				signPath:'',
				form: {
					sortNo: 1,
					sealSignId:"",
					title: "",
					accountId: "",
					sign:'',
					password:'',
					status: 1,
					model:'0',
					remark: ""
				},
				//验证规则
				rules: {
					sortNo: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					title: [
						{required: true, message: '请输入签名标题'}
					],
					accountId: [
						{required: true, message: '请选择签名使用人',trigger: 'change'}
					],
					password:[
						{required: true, message: '请设定使用密码'}
					],
				},
			}
		},
		mounted() {

		},
		methods: {
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			getFileName(file)
			{
				this.form.sign = file.data.name
			},
			//表单提交方法
			submit(){
				if(this.password1!=this.password)
				{
					this.$alert("两次输入的密码不一致", "提示", {type: 'error'})
					return;
				}
				if(this.mode=="add")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.bpm.bpmSealSign.insert.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('success', this.form, this.mode)
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}else if(this.mode=="edit")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.bpm.bpmSealSign.update.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('success', this.form, this.mode)
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}
			},
			setData(data){
				this.form.sortNo = data.sortNo
				this.form.sealSignId = data.sealSignId
				this.form.title = data.title
				this.form.sign = data.sign
				this.signPath = config.API_URL+"/get/file/getBpmSign?img="+data.sign
				this.form.accountId = data.accountId
				this.form.password = data.password
				this.form.status = data.status
				this.form.model = data.model
				this.form.remark = data.remark
			}
		}
	}
</script>

<style scoped>
:deep(.el-upload--picture-card){
	display: contents;
}
</style>
